﻿using Common.Contract;
using Common.Contract.Customer;
using Common.Contract.Customer.Request;
using Common.Contract.SaleOut;
using Common.Contract.SaleOut.Request;
using Common.Contract.Warranty.Request;
using Common.Service;
using Common.Service.Command;
using Common.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Warranty.Configuration;
using Warranty.Model.Interface;
using Warranty.Model.Model;

namespace Warranty.ServiceCommand
{
    public class UpdateWarrantyMasterServiceCommand : ProviderServiceCommand<IProviderFactory, UpdateWarrantyMasterRequest, ResponseRecord>
    {
        public UpdateWarrantyMasterServiceCommand(IProviderFactory provider) : base(provider) { }

        protected override void Validate(UpdateWarrantyMasterRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
        }

        protected override bool OnExecute(ResponseRecord response)
        {
            var record = Request.Record;
            var obj = new WarrantyMaster()
            {
                Amt = record.Amt,
                CloseFlag = record.CloseFlag.HasValue? record.CloseFlag.Value.ToBooleanString():null,
                ClosePeriod = record.ClosePeriod,
                CloseUser = record.CloseUser,
                CreateDate = record.CreateDate,
                EndDate = record.EndDate, 
                EndUserCell = record.EndUserCell,
                IncentiveCfmDate = record.IncentiveCfmDate,
                IncentiveCfmFlag = record.IncentiveCfmFlag.HasValue? record.IncentiveCfmFlag.Value.ToBooleanString():null,
                IncentiveCfmPeriod = record.IncentiveCfmPeriod,
                IncentiveCfmUser = record.IncentiveCfmUser, 
                LastUpdateDate = record.LastUpdateDate, 
                Model = record.Model,
                Point = record.Point,
                SellinDate = record.SellinDate,
                SelloutDate = record.SelloutDate,
                SelloutRespMsg = record.SelloutRespMsg,
                SelloutRespType = record.SelloutRespType,
                SelloutTime = record.SelloutTime,
                SerialNo = record.SerialNumber, 
                ShopCell = record.ShopCell,
                ShopCode = record.ShopCode,
                StartDate = record.StartDate,
                Suffix = record.Suffix, 
                SmsYn = record.SmsYn.HasValue ? record.SmsYn.Value.ToBooleanString() : null,
                SoTransferFlag = record.TransferFlag.HasValue ? record.TransferFlag.Value.ToBooleanString() : null,
                TransferFlag = record.TransferFlag.HasValue ? record.TransferFlag.Value.ToBooleanString() : null,
            };
            return Providers.WarrantyMaster.Update(obj);
        }
    }
}
